package com.victualler.core.config.permission.repository.impl;

import com.victualler.core.base.repository.impl.BaseQueryRepositoryImpl;
import com.victualler.core.config.permission.entity.PermissionInfoDao;
import com.victualler.core.config.permission.entity.RolePermissionInfoDao;
import com.victualler.core.config.permission.repository.PermissionInfoQueryRepository;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Guoshichang
 * @function 封装用于复杂sql查询的API. 为每个实体的repository层提供便利操作
 */
@Component
public class PermissionInfoQueryRepositoryImpl extends BaseQueryRepositoryImpl<PermissionInfoDao,Long> implements PermissionInfoQueryRepository {
    /**
     * @author Guoshichang
     * @since 19:06 on 2018/7/6.
     * @param roleIds
     * @Function 根据角色列表获取权限列表
     */
    @Override
    public List<PermissionInfoDao> getPermissionInfoByRoleId(List<Long> roleIds) {
        String perTableName = this.getTableName(PermissionInfoDao.class);
        String rolePerTableName = this.getTableName(RolePermissionInfoDao.class);
        String sql = "select * from "+perTableName+" where id in ( select permission_Id  from "+rolePerTableName+" where role_Id in (:roleIds) )";
        Map<String ,Object> params = new HashMap<>();
        params.put("roleIds",roleIds);
        return this.queryListBySql(sql,params);
    }

    /**
     * @author Guoshichang
     * @since 21:12 on 2018/9/9.
     * @param roleId 双
     * @return List
     * @Function
     */
    public List<PermissionInfoDao> notInByRoleId(Long roleId) {
        String perTableName = this.getTableName(PermissionInfoDao.class);
        String rolePerTableName = this.getTableName(RolePermissionInfoDao.class);
        String sql = "select * from "+perTableName+" where id not in ( select permission_Id  from "+rolePerTableName+" where role_Id = ? )";
        return this.queryListBySql(sql,new Object[]{roleId});
    }

}
